#!/usr/bin/env python
# coding=utf-8
from optparse import OptionParser
import ConfigParser,os,sys
from tempfile import mkstemp
import pysvn
import filecmp
import subprocess

'''通过命令行获取参数'''
parser = OptionParser("%prog [options] arg1", version="%prog v1.0")
parser.description = "this is a python use svn config manager system."
parser.add_option("-f", "--configfile", dest="configfile",help="config file", metavar="FILE",default="")
(options, args) = parser.parse_args()

'''配置文件获取'''
ini_file = options.configfile
if not os.path.isfile(ini_file):
	parser.error("not config file!")
	sys.exit()
config = ConfigParser.ConfigParser()
config.readfp(open(ini_file))

'''配置文件检查'''
def check_config(section,option):
	if config.has_option(section,option) == False:
		print "config file= %s , section:%s or option:%s is not set." % (ini_file,section,option)
		sys.exit()
	return config.get(section,option)
def check_section(section):
	if config.has_section(section) == False:
		print "config file= %s , section:%s is not set" % (ini_file,section)
		sys.exit()

#关于svn的配置
svn_section = "svn"
check_section(svn_section)
#获得配置文件中svn源路径
svn_root = check_config(svn_section,"svn_root")
#获得配置文件中svn路径
project_dir = check_config(svn_section,"project_dir")

'''
#获得配置文件中 svn保存路径
save_path = check_config(svn_section,"save_path")
#获得配置文件中 最后版本文件名
version_file = check_config(svn_section,"version_file")
'''

#获得配置文件中 svn用户名
svn_user = check_config(svn_section,"svn_user")
#获得配置文件中 svn密码
svn_pwd = check_config(svn_section,"svn_pwd")

check_section("check_file")
check_section("copy_path")
check_section("restart_cmd")

#检测 是否配置的检测中的配置是否匹配
check_file_list = config.options("check_file")
copy_path_list = config.options("copy_path")
restart_cmd_list = config.options("restart_cmd")

conf_file_hash = {}

for conf_file in check_file_list:
	conf_temp = {}
	conf_temp['check_file'] = config.get('check_file',conf_file)
	try:
		copy_path_list.index(conf_file)
		conf_temp['copy_path'] = config.get('copy_path',conf_file)
		if not os.path.isfile(conf_temp['copy_path']):
			print "config file %s,section:%s,option:%s value:%s is not file." % (ini_file,"copy_path",conf_file,conf_temp['copy_path'])
			sys.exit()
	except ValueError:
		print "config file %s,section:%s,option:%s is not set." % (ini_file,"copy_path",conf_file)
		sys.exit()
	try:
		restart_cmd_list.index(conf_file)
		conf_temp['cmd'] = config.get('restart_cmd',conf_file)
	except ValueError:
		print "config file %s,section:%s,option:%s is not set." % (ini_file,"restart_cmd",conf_file)
		sys.exit()
	conf_file_hash[conf_file] = conf_temp
#print conf_file_hash

#svn 检测

#svn 登陆回调函数
def svn_login(realm,username,may_save):
	return True,svn_user,svn_pwd,False

client = pysvn.Client()
client.callback_get_login =svn_login

svn_host = svn_root+project_dir

conf_file_list = conf_file_hash.keys()

change_list=[]
for conf_key in conf_file_list:
	#svn_conf_file = svn_host+conf_file_list[conf_key]['check_file']
	svn_conf_file = svn_host+conf_file_hash[conf_key]['check_file']
	cur_conf_file = conf_file_hash[conf_key]['copy_path']
	#print svn_conf_file
	#print cur_conf_file
	try:
		temp_file_content=client.cat(svn_conf_file)
	except:
		print "svn server file: %s is not ." % (svn_conf_file)
		sys.exit()
	#建立临时文件为比对做准备
	(temp_fd, temp_file_name) = mkstemp()
	os.write(temp_fd, temp_file_content)
	os.close(temp_fd)
	#print temp_file_name
	if filecmp.cmp(cur_conf_file,temp_file_name) == False:
		#替换文件
		open(cur_conf_file, "wb").write(open(temp_file_name, "rb").read())
		change_list.append(conf_file_hash[conf_key]['cmd'])

#print conf_file_hash
#print conf_file_list
for run_cmd in change_list:
	if len(run_cmd) >1:
		data=subprocess.Popen(run_cmd,stdout=subprocess.PIPE,shell=True)
		#print data.stdout.read().splitlines()
